/**
 * Mark Fedor
 * Tony Greseth
 * CS 5551 User Interface Design
 * Section 1
 * Sai Chittilla
 * Yahtzee Program
 */
package Yahtzee;
import java.io.*;

/**
 * This class makes the scoreboard object which is shown on the UI, it holds all the scores
 * for each category.
 * @author Mark
 */
public class Scoreboard implements Serializable, Cloneable  {
    
/**
 * Creates the scoreboard which initializes all of the fields to 0
 */

public Scoreboard() {
    
    /* scores[0] = 1's
     * scores[1] = 2's
     * scores[2] = 3's
     * scores[3] = 4's
     * scores[4] = 5's
     * scores[5] = 6's
     * scores[6] = 3 of kind
     * scores[7] = 4 of kind
     * scores[8] = Full House
     * scores[9] = Small Straight
     * scores[10] = Large Straight
     * scores[11] = Yahtzee
     * scores[12] = Chance
     */
    scores = new int[13];
    reset();
}

/**
 * updates the board
 * @param move updates the board based on what was sent
 */
public void update(Move move)
{
    {
        scores[move.getIndex()] = move.getScore();
    }
}
/**
 * updates the board based on the index passed
 * @param index value to be changed
 * @throws IndexOutOfBoundsException
 */
public void update(int index) throws IndexOutOfBoundsException
{
    if (index == 0)
    {
       scores[index] = Dice.getOnes();
       ones = true;
    }
    else if (index == 1)
    {
        scores[index] = Dice.getTwos();
        twos = true;
    }
    else if (index == 2)
    {
        scores[index] = Dice.getThrees();
        threes = true;
    }
    else if (index == 3)
    {
        scores[index] = Dice.getFours();
        fours = true;
    }
    else if (index == 4)
    {
        scores[index] = Dice.getFives();
        fives = true;
    }
    else if (index == 5)
    {
        scores[index] = Dice.getSixes();
        sixes = true;
    }
    else if (index == 6)
    {
        scores[index] = Dice.getThreeOfAKind();
        threeKind = true;
    }
    else if (index == 7)
    {
        scores[index] = Dice.getFourOfAKind();
        fourKind = true;
    }
    else if (index == 8)
    {
        scores[index] = Dice.getFullHouse();
        fullHouse = true;
    }
    else if (index == 9)
    {
        scores[index] = Dice.getSmallStraight();
        smallStraight = true;
    }
    else if (index == 10)
    {
        scores[index] = Dice.getLargeStraight();
        largeStraight = true;
    }
    else if (index == 11)
    {
        scores[index] = Dice.getYahtzee();
        yahtzee = true;
    }
    else if (index == 12)
    {
        scores[index] = Dice.getChance();
        chance = true;
    }
    else
        throw new IndexOutOfBoundsException();

}

/**
 * calcuates the total score of top section
 */
private void sumTop() {
    sumTop = 0;
    for (int i =0; i < 6; i++)
    {
        sumTop += scores[i];
    }
}
/**
 * gets top score total
 * @return the top score
 */
public int getTop()
{
    sumTop();
    return sumTop;
}
/**
 * gets bottom score total
 * @return the bottom score
 */
public int getBottom()
{
    sumBottom();
    return sumBottom;
}
/**
 * gets the yahtzeecount and determines bonus amount
 * @return the amount of yahtzee bonus
 */
public int getYahtzeeBonus()
{
    if (yahtzeeCount > 1)
    return (yahtzeeCount-1) * 100;
    else return 0;
}
/**
 * determines if bonus is present applies if it is
 * @return the amount of bonus applied
 */
public int getTopBonus()
{
    if (sumTop > 62)
        return 36;
    else
        return 0;
}

private void sumBottom()
{
    sumBottom = 0;
    for (int i = 6; i < 13; i++)
    {
        sumBottom += scores[i];
    }

}

/**
 * determines if the scores result in a 36 point bonus ie the top sum is greater than 63 pts.
 * @return true if bonus false if not
 */
public boolean isTopBonus() {
    sumTop();
    if (sumTop > 62)
        return true;
    else
        return false;
}

/**
 * totals up the final score
 */
private void calcTotal() {
    yahtzeeBonus = 0;
    topBonus = 0;
    total = 0;
    if (yahtzeeCount > 0) // blocks the first call to yahtzeecount
        // so it doesn't result in a negative number for total
        yahtzeeBonus = (yahtzeeCount-1) * 100;
    if (isTopBonus())
    {
        topBonus = 36;
    }
    for (int i = 0; i < 13; i++)
    {
        total += scores[i];
    }
    total = total + yahtzeeBonus + topBonus;
}
/**
 * accessor method which gets the total for the yahtzee game
 * @return the total for the game
 */
public int getTotal()
{
    calcTotal();
    return total;
}


/**
 * Clears the scoreboard of scores
 */
public void reset() {
    for (int i = 0; i < 13; i++)
    {
        scores[i] = 0;
    }
    yahtzeeCount = 0;
    ones = false;
    twos = false;
    threes = false;
    fours = false;
    fives = false;
    sixes = false;
    threeKind = false;
    fourKind = false;
    fullHouse = false;
    smallStraight = false;
    largeStraight = false;
    yahtzee = false;
    chance = false;

}

public int getScore(int index)
{
    return scores[index];
}

public int[] getBoard()
{
    return scores;
}

// set up the socre based on the index values
public void setScores(int index, int score) throws IndexOutOfBoundsException
{
    if (index == 0)
    {
       scores[index] = score;
       ones = true;
    }
    else if (index == 1)
    {
        scores[index] = score;
        twos = true;
    }
    else if (index == 2)
    {
        scores[index] = score;
        threes = true;
    }
    else if (index == 3)
    {
        scores[index] = score;
        fours = true;
    }
    else if (index == 4)
    {
        scores[index] = score;
        fives = true;
    }
    else if (index == 5)
    {
        scores[index] = score;
        sixes = true;
    }
    else if (index == 6)
    {
        scores[index] = score;
        threeKind = true;
    }
    else if (index == 7)
    {
        scores[index] = score;
        fourKind = true;
    }
    else if (index == 8)
    {
        scores[index] = score;
        fullHouse = true;
    }
    else if (index == 9)
    {
        scores[index] = score;
        smallStraight = true;
    }
    else if (index == 10)
    {
        scores[index] = score;
        largeStraight = true;
    }
    else if (index == 11)
    {
        scores[index] = score;
        yahtzee = true;
    }
    else if (index == 12)
    {
        scores[index] = score;
        chance = true;
    }
    else
        throw new IndexOutOfBoundsException();
}

// all variables used throughout the class
public int[] scores;

private int sumTop;
private int sumBottom;
private int total;
public boolean ones, twos, threes, fours, fives, sixes,
            threeKind, fourKind, fullHouse, smallStraight, largeStraight,
            yahtzee, chance;

private int yahtzeeBonus;
private int topBonus;
public int yahtzeeCount = 0;

}